﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects;
using Course.Models;

namespace Course.DAO
{
    public class ArticleDAO
    {

        /// <summary>
        /// 获取一条文章信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Article GetOne(int id)
        {
            return DBHelper.Entity.Articles.SingleOrDefault(a => a.ArticleID == id);
        }

        /// <summary>
        /// 获取所有文章
        /// </summary>
        /// <returns></returns>
        public IQueryable<Article> GetAll()
        {
            return DBHelper.Entity.Articles.Select(a => a).OrderBy(a => a.OrderNum);
        }

        /// <summary>
        /// 添加一篇文章
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public bool AddOne(Article article)
        {
            try
            {
                DBHelper.Entity.AddToArticles(article);
                int count = DBHelper.Entity.SaveChanges();

                if (count != -1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }

        /// <summary>
        /// 修改一篇文章
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public bool EditOne(Article article)
        {
            try
            {
                var query = DBHelper.Entity.Articles.SingleOrDefault(a => a.ArticleID == article.ArticleID);
                query.ATitle = article.ATitle;
                query.Author = article.Author;
                query.AContent = article.AContent;
                query.CategoryID = article.CategoryID;
                query.OrderNum = article.OrderNum;
                int count = DBHelper.Entity.SaveChanges();

                if (count != -1)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }

        /// <summary>
        /// 根据顺序获得文章
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public Article GetOneByOrder(int order)
        {
            return DBHelper.Entity.Articles.SingleOrDefault(a => a.OrderNum == order);
        }

        /// <summary>
        /// 删除一篇文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string DeleteOne(int id)
        {
            try
            {
                var query = DBHelper.Entity.Articles.SingleOrDefault(a => a.ArticleID == id);
                if (query.Downloads.Count > 0)
                {
                    return "无法删除!";
                }
                else
                {
                    DBHelper.Entity.DeleteObject(query);
                    int count = DBHelper.Entity.SaveChanges();
                    if (count != -1)
                    {
                        return "删除成功!";
                    }
                    else
                    {
                        return "删除出错!";
                    }
                }
            }
            catch (Exception)
            {
                return "删除出错!";
                throw;
            }
        }

        /// <summary>
        /// 根据分类获得一组文章
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public IQueryable<Article> GetArticles(string categoryName)
        {
            try
            {
                //var query = from a in DBHelper.Entity.Articles
                //            join c in DBHelper.Entity.Categories on a.CategoryID equals c.CategoryID
                //            where c.CName == categoryName
                //            select a;
                var query = DBHelper.Entity.Articles.Where(a => a.Category.CName == categoryName);
                return query;
            }
            catch (Exception)
            {
                return null;
                throw;
            }


        }


        /// <summary>
        /// 根据分类获得一篇文章
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public Article GetOne(string categoryName)
        {
            var query = DBHelper.Entity.Articles.SingleOrDefault(a => a.Category.CName == categoryName);

            return query;
        }

        /// <summary>
        /// 根据标题获取文章
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Article GetOneByTitle(string name)
        {
            try
            {
                var query = DBHelper.Entity.Articles.SingleOrDefault(a => a.ATitle == name);
                var aa = DBHelper.Entity.Articles.Count();
                return query;
            }
            catch (Exception)
            {

                return null;
                throw;
            }
        }

        public IQueryable<Article> GetSomeArticles(int cid)
        {
            try
            {
                var query = DBHelper.Entity.Articles.Where(a => a.CategoryID == cid).OrderBy(a => a.OrderNum);
                return query;
            }
            catch (Exception)
            {
                return null;
                throw;
            }
        }
    }
}